/**
 * 
 */
package com.cnhaimei.vod.logic;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.cnhaimei.base.TransparentActivity;
import com.cnhaimei.vod.R;
import com.cnhaimei.vod.logic.EventsManager.EventsListener;
import com.cnhaimei.vod.net.XVod;
import com.cnhaimei.vod.net.dc.Player;

/**
 * 音量控制
 * 
 * @author jin001.wu@gmail.com <br>
 * @date 2011-7-30
 * 
 */
public class VolumeActivity extends TransparentActivity implements
		EventsListener {

	TextView mNum;
	View mSilent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.volume_ctr);

		//mNum = (TextView) findViewById(R.id.edit);
		mSilent = findViewById(R.id.silent);
		
		setClickable(R.id.reduse, R.id.add, R.id.silent);
	}

	@Override
	protected void onResume() {
		super.onResume();
		refreshPlayerState();
		XVod.G().registerListener(EventsManager.Event.PLYER_STATE, this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		XVod.G().unRegisterListener(EventsManager.Event.PLYER_STATE, this);
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	public void onEvent(int event, Object obj, boolean isSuc) {
		switch (event) {
		case EventsManager.Event.PLYER_STATE:
			refreshPlayerState();
			break;

		default:
			break;
		}

	}

	@Override
	public void onClick(View v) {
		final Player p = Player.G();
		int id = v.getId();
		switch (id) {
		case R.id.reduse: {
			int vol = p.getAudioVolume();
			if (vol > p.getAudioMin()) {
				XVod.G().setPlayerSet(Player.PLAYER_SET_VOLUM, vol -p.getAudioScale()); // 请求减少音量
			}
		}
			break;
		case R.id.add: {
			int vol = p.getAudioVolume();
			if (vol < p.getAudioMax()) {
				XVod.G().setPlayerSet(Player.PLAYER_SET_VOLUM, vol + p.getAudioScale()); // 请求增加
			}
		}
			break;
		case R.id.silent:
			XVod.G().setPlayerState(Player.PLAYER_SET_SILENT); // 请求静音
			break;
		default:
			super.onClick(v);
			break;
		}
	}
	
	public void refreshPlayerState() {
		final Player p = Player.G();
		//mNum.setText(String.valueOf(p.getAudioVolume()));
		boolean isSilent = p.getSlient() == 1 ? true : false;
		if (isSilent) {
			mSilent.setBackgroundResource(R.drawable.volume_ctr_silent_active);
		} else {
			mSilent.setBackgroundResource(R.drawable.volume_ctr_silent);
		}
	}

}
